pip install pyecharts
Exploring Pyecharts in Python
Pyecharts is a Python library, serving as a class library that allows you to effortlessly generate interactive and visually compelling charts using ECharts, an open-source data visualization JavaScript library developed by Baidu. With Pyecharts, users can easily create dynamic and customizable charts for data visualization in web applications or Jupyter notebooks.
Getting Started
To begin using the pyecharts library, start by installing it on your system:
Importing Libraries
import pandas as pd
#For Bar chart
from pyecharts import options as opts
from pyecharts.charts import Bar
#For Funnel chart
from pyecharts import Funnel
Importing Dataset
= pd.read_csv('Data.csv')
df df.head()
booking_id | property_id | booking_date | Month Name | Day Name | weekday | no_guests | room_category | booking_platform | ratings_given | ... | Revenue_lost | Week of Year | No of Days | dim_rooms | property_name | category | city | successful_bookings | capacity | Unsuccessful_bookings | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | May012216558RT11 | 16558 | 27-04-22 | April | Wednesday | weeke day | 3 | RT1 | direct online | 1.0 | ... | 0 | 18 | 1 | Standard | Atliq Grands | Luxury | Delhi | 18 | 19 | 1 |
1 | May012216558RT12 | 16558 | 30-04-22 | April | Saturday | weekend day | 2 | RT1 | others | NaN | ... | 5460 | 18 | 1 | Standard | Atliq Grands | Luxury | Delhi | 18 | 19 | 1 |
2 | May012216558RT13 | 16558 | 28-04-22 | April | Thursday | weeke day | 2 | RT1 | logtrip | 5.0 | ... | 0 | 18 | 3 | Standard | Atliq Grands | Luxury | Delhi | 18 | 19 | 1 |
3 | May012216558RT14 | 16558 | 28-04-22 | April | Thursday | weeke day | 2 | RT1 | others | NaN | ... | 5460 | 18 | 1 | Standard | Atliq Grands | Luxury | Delhi | 18 | 19 | 1 |
4 | May012216558RT15 | 16558 | 27-04-22 | April | Wednesday | weeke day | 4 | RT1 | direct online | 5.0 | ... | 0 | 18 | 1 | Standard | Atliq Grands | Luxury | Delhi | 18 | 19 | 1 |
5 rows × 24 columns
Let’s try out some functions:
Bar() - It creates a bar chart
# Total revenue generated & lost
= df.groupby('property_name').agg({'revenue_generated': 'sum','Revenue_lost': 'sum'}).reset_index()
property_metrics
#Renaming column names
=['Property Name', 'Total Revenue Generated','Total Revenue Lost']
property_metrics.columns
#Plotting bar chart
=(Bar()
bar_chart'Property Name'].tolist())
.add_xaxis(property_metrics['Total Revenue Generated',property_metrics['Total Revenue Generated'].round(0).tolist())
.add_yaxis('Total Revenue Lost',property_metrics['Total Revenue Lost'].round(0).tolist())
.add_yaxis(=opts.LabelOpts(position='top'))
.set_series_opts(label_opts
.set_global_opts(=opts.TitleOpts(title='Property Revenue Overview',
title_opts='Analyzing Total Revenue Dynamics')
subtitle
)
) bar_chart.render_notebook()
Funnel()- It creates a funnel chart
#For funnel, the below version is required
==0.5.11 pip install pyecharts
#Booking Volume by Day of the Week
= df.groupby('Day Name').agg({'booking_id':'count'}).reset_index()
booking_day
booking_day
#Plotting Funnel chart
= Funnel('Booking Volume by Day of the Week')
Funnel 'Total Bookings', booking_day['Day Name'], booking_day['booking_id'], is_label_show = True,label_pos = 'inside',
Funnel.add(='vertical', legend_pos='right') legend_orient
Features of Pyecharts:
Rich Chart Types: It offers diverse charts for flexible and enhanced data visualization.
Customization Options: It allows extensive customization for tailored chart appearances.
Interactive Visualizations: It enables exploration with zooming, panning, and hovering, providing deeper insights.
Declarative API: It simplifies chart creation with a concise declarative API.
ECharts Backend: It is built on ECharts, providing access to a comprehensive set of features.
Comparison of Python Data Visualization Libraries
Feature | Matplotlib | Seaborn | Pyecharts |
---|---|---|---|
Type of Library | 2-D plotting library | Data visualization framework | Echarts chart class library |
Integration | Python scripts, notebooks, web servers | Integrated with NumPy and pandas | Directly docked with Python |
Visualization Capabilities | Various plot types, customizable | Statistical visuals with datasets | Customizable charts with Echarts |
Output Format | Image files, interactive plots in notebooks | Interactive plots for analysis | HTML files locally |
Key Features | Widely used, platform compatible | High-level interface, pandas friendly | Configurable, graphical effects |
Conclusion:
Pyecharts stands out as a versatile and user-friendly Python library for data visualization. Leveraging the powerful ECharts backend, Pyecharts provides a comprehensive set of features, positioning it as a valuable tool for diverse applications. This, combined with its intuitive interface, makes Pyecharts a compelling choice for those seeking efficient and visually appealing data representations, catering to both beginners and experienced users alike.